//
//  ZZCBaseScriptMeaasgeHandler.swift
//  ewgShop
//
//  Created by zzc on 2019/8/5.
//  Copyright © 2019 zzc. All rights reserved.
//

import UIKit
import Foundation
import WebKit
//import


class ZZCBaseScriptMeaasgeHandler: NSObject,WKScriptMessageHandler {
    
    
    weak var viewController : ZZCCordovaBaseWebViewVC?
    
    let OBSERVE_READY_STATE_MESSAGE_HANDLER_NAME = "ObserveReadyState"
    let QUERY_READY_STATE_MESSAGE_HANDLER_NAME = "QueryReadyState"
    let QUERY_REDIRECT_STATE_MESSAGE_HANDLER_NAME = "QueryRedirectState"
    
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
        switch message.name {
        case OBSERVE_READY_STATE_MESSAGE_HANDLER_NAME:
            handleObserveReadyStateMessage(message: message)
            break;
        case QUERY_READY_STATE_MESSAGE_HANDLER_NAME :
            handleQueryReadyStateMessage(message: message)
            break;
        case QUERY_REDIRECT_STATE_MESSAGE_HANDLER_NAME:
            handleQueryRedirectStateMessage(message: message)
            break;
        default:
            break;
        }
    }
    
    
    func handleObserveReadyStateMessage(message:WKScriptMessage){
        
        let vc = self.viewController
        if (message.body as! NSString).isEqual(to: "interactive") ||
            (message.body as! NSString).isEqual(to: "complete"){
            // 结束loading
            vc?.wkWebView.evaluateJavaScript((vc!.jsOfLocationHref()), completionHandler: ({ (href:String, error : NSError) in
                    vc?.currentHref = href
                } as! (Any?, Error?) -> Void))
        }
        
        if (message.body as! NSString).isEqual(to: "interactive") {
            vc?.readyState = ZZCWKWebViewReadyState.Interactive
            CDVTimer.stop(NSString.init(format: "load interactive: %@", vc!.startPage) as String)
        }else if(message.body as! NSString).isEqual(to: "complete"){
            vc?.readyState = ZZCWKWebViewReadyState.Complete
            CDVTimer.stop(NSString.init(format: "load complete: %@", vc!.startPage) as String)
        }else{
            vc?.readyState = ZZCWKWebViewReadyState.Loading
        }
    }
    
    func handleQueryReadyStateMessage(message:WKScriptMessage){
        let vc = self.viewController
        if(message.body as! NSString).isEqual(to: "loading"){
//            vc?.wkWebView.evaluateJavaScript((vc!.jsOfLocationHref()), completionHandler: ({ (href:String, error : NSError) in
//                vc?.currentHref = href
//                })
            let js = vc?.jsOfLocationHref()
            vc?.wkWebView.evaluateJavaScript(js!, completionHandler: { (href:Any?, error:Error?) in
                vc?.currentHref = href as? String
            })
        }
    }
    
    func handleQueryRedirectStateMessage(message:WKScriptMessage){
        let vc = self.viewController
        vc?.redirectPage = NSString.init(format: "%@", message.body as! CVarArg).boolValue
    }
    

}
